(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.sysNote.createContinueController', createContinueController);
	createContinueController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'sysNoteService' ];
	function createContinueController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, sysNoteService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.sysNote = {};

		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.noteMsgsCfg.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.sysNote.noteMsgs';
			authService.createContinue(params).then(function(response) {
				if (response.data && response.data.model && response.data.task) {
					ctrl.sysNote = response.data.model;
					ctrl.sysNote.noteMsgs = ctrl.sysNote.noteMsgs || [];
					ctrl.sysNote.taskId = response.data.task.taskId;
					ctrl.localOptions = response.data.model.localList;
				}
			});
		};

		ctrl.setLocaleCode = function() {
			var addlc = ctrl.sysNote.noteMsgs;
			if (addlc.length > 0) {
				if (addlc.length >= 2) {
					for (var i = 0; i < addlc.length - 1; i++) {
						if (addlc[i].localeCode != null
								&& addlc[i].localeCode.trim() != "") {
							if (addlc[i].localeCode == addlc[addlc.length - 1].localeCode) {
								addlc[addlc.length - 1].localeCode = "";
								modalService.openErrorDialogs({
									messages : [ 'js.info.sysnote.value-existed' ]
								});
								return;
							}
						}
					}
				}
			}
		};
		
		ctrl.createContinueSave = function() {
			return authService.createContinueSave(ctrl.sysNote).then(saveSuccessful, saveFailed);
		};

		ctrl.createContinueSubmit = function() {
			return authService.createContinueSubmit(ctrl.sysNote).then(submitSuccessful, submitFailed);
		};
		
		ctrl.createContinueCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.sysNote.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createContinueCancel(ctrl.sysNote).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};
		
		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.sysnote.list',
					'state' : 'cda.sysNote.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				angular.extend(ctrl.sysNote, response.data.model);
			}else if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.sysnote.list',
					'state' : 'cda.sysNote.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedMessages = selectedRows;
		}
	}
})(angular);